package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityAd;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoAD;
import com.ocom.payconfig.service.ADService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ADServiceImpl extends ServiceImpl<IDaoAD, EntityAd> implements ADService {

    @Autowired
    private IDaoAD iDaoAD;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public int add(Long comId, EntityAd entityAd) {
        Date date=new Date();
        entityAd.setCreateTime(date);
        entityAd.setUpdateTime(date);
        return iDaoAD.insert(entityAd);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public int delAd(Long comId, Long id) {
        QueryWrapper<EntityAd> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return iDaoAD.delete(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntityAd> adList(Long comId, Integer deviceType, Integer type) {
        QueryWrapper<EntityAd> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        if (deviceType !=null){
            queryWrapper.eq("device_type",deviceType);
        }
        if (type !=null){
            queryWrapper.eq("type",type);
        }
        queryWrapper.orderByDesc("sort");
        return iDaoAD.selectList(queryWrapper);
    }
}
